<!DOCTYPE html>

<html>
<head>
  <title>mail.coffee</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div id="container">
    <div id="background"></div>
    
      <ul id="jump_to">
        <li>
          <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
          <a class="small" href="javascript:void(0);">+</a>
          <div id="jump_wrapper">
          <div id="jump_page">
            
              
              <a class="source" href="configure.html">
                configure.coffee
              </a>
            
              
              <a class="source" href="dataParser.html">
                dataParser.coffee
              </a>
            
              
              <a class="source" href="database.html">
                database.coffee
              </a>
            
              
              <a class="source" href="logger.html">
                logger.coffee
              </a>
            
              
              <a class="source" href="mail.html">
                mail.coffee
              </a>
            
              
              <a class="source" href="messages.html">
                messages.coffee
              </a>
            
              
              <a class="source" href="routes.html">
                routes.coffee
              </a>
            
              
              <a class="source" href="settings.html">
                settings.coffee
              </a>
            
              
              <a class="source" href="settings.default.html">
                settings.default.coffee
              </a>
            
              
              <a class="source" href="twitter.html">
                twitter.coffee
              </a>
            
          </div>
        </li>
      </ul>
    
    <ul class="sections">
        
          <li id="title">
              <div class="annotation">
                  <h1>mail.coffee</h1>
              </div>
          </li>
        
        
        
        <li id="section-1">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-1">&#182;</a>
              </div>
              <h2>SERVER MAIL</h2>

            </div>
            
        </li>
        
        
        <li id="section-2">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-2">&#182;</a>
              </div>
              <p>Sends and manages emails on the app.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="class"><span class="keyword">class</span> <span class="title">Mail</span></span>

    fs = require <span class="string">"fs"</span>
    logger = require <span class="string">"./logger.coffee"</span>
    mailer = require <span class="string">"nodemailer"</span>
    settings = require <span class="string">"./settings.coffee"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-3">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-3">&#182;</a>
              </div>
              <p>SMTP object will be instantiated on <code>init</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    smtp = <span class="literal">null</span></pre></div></div>
            
        </li>
        
        
        <li id="section-4">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-4">&#182;</a>
              </div>
              <p>Templates cache to avoid disk reads.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    templateCache = {}</pre></div></div>
            
        </li>
        
        
        <li id="section-5">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-5">&#182;</a>
              </div>
              <h2>INIT</h2>

            </div>
            
        </li>
        
        
        <li id="section-6">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-6">&#182;</a>
              </div>
              <p>Init the SMTP transport, but only if <code>smtpHost</code> and <code>smtpPort</code> are defined on the <a href="settings.html">settings</a>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    init: =&gt;
        <span class="keyword">if</span> settings.Email.smtpHost? <span class="keyword">and</span> settings.Email.smtpHost <span class="keyword">isnt</span> <span class="string">""</span> <span class="keyword">and</span> settings.Email.smtpPort &gt; <span class="number">0</span>
            smtp = mailer.createTransport <span class="string">"SMTP"</span>,
                debug: settings.General.debug,
                host: settings.Email.smtpHost,
                port: settings.Email.smtpPort,
                secureConnection: settings.Email.smtpSecure,
                auth:
                    user: settings.Email.smtpUser,
                    pass: settings.Email.smtpPassword
            <span class="keyword">if</span> settings.General.debug
                logger.info <span class="string">"Mail.init"</span>, settings.Email.smtpHost, settings.Email.smtpPort
        <span class="keyword">else</span> <span class="keyword">if</span> settings.General.debug
            logger.info <span class="string">"Mail.init"</span>, <span class="string">"No SMTP host/port specified."</span>, <span class="string">"No emails will be sent out!"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-7">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-7">&#182;</a>
              </div>
              <h2>OUTBOUND</h2>

            </div>
            
        </li>
        
        
        <li id="section-8">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-8">&#182;</a>
              </div>
              <p>Sends an email to the specified address. The <code>obj</code> will be parsed and transformed
to a HTML formatted message.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    send: (obj, subject, toAddress, fromAddress) -&gt;
        <span class="keyword">if</span> <span class="keyword">not</span> smtp?
            <span class="keyword">if</span> settings.General.debug
                logger.warn <span class="string">"Mail.send"</span>, <span class="string">"SMTP transport wasn't initiated. Abort!"</span>, subject, <span class="string">"to <span class="subst">#{toAddress}</span>"</span>
            <span class="keyword">return</span>

        <span class="keyword">if</span> <span class="keyword">not</span> obj?
            <span class="keyword">if</span> settings.General.debug
                logger.warn <span class="string">"Mail.send"</span>, <span class="string">"Parameter obj is null. Abort!"</span>, subject, <span class="string">"to <span class="subst">#{toAddress}</span>"</span>
            <span class="keyword">return</span></pre></div></div>
            
        </li>
        
        
        <li id="section-9">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-9">&#182;</a>
              </div>
              <p>Set from to default address if no <code>fromAddress</code> was set and create the options object.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        fromAddress = <span class="string">"<span class="subst">#{settings.General.appTitle}</span> &lt;<span class="subst">#{settings.Email.from}</span>&gt;"</span> <span class="keyword">if</span> <span class="keyword">not</span> fromAddress?
        options = {}

        <span class="keyword">if</span> toAddress.indexOf(<span class="string">"&lt;"</span>) &lt; <span class="number">3</span>
            toName = toAddress
        <span class="keyword">else</span>
            toName = toAddress.substring <span class="number">0</span>, toAddress.indexOf(<span class="string">"&lt;"</span>) - <span class="number">1</span></pre></div></div>
            
        </li>
        
        
        <li id="section-10">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-10">&#182;</a>
              </div>
              <p>Check if <code>obj</code> is a countdown and generate the HTML based on its details.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">if</span> obj.attributes? <span class="keyword">and</span> obj.attributes.friendlyUrl? <span class="keyword">and</span> obj.attributes.targetDate?
            html = <span class="property">@getTemplate</span> <span class="string">"countdown"</span>
            html = <span class="property">@parseTemplate</span> html, obj.attributes</pre></div></div>
            
        </li>
        
        
        <li id="section-11">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-11">&#182;</a>
              </div>
              <p>Otherwise assume <code>obj</code> is the message itself.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">else</span>
            html = obj.toString()</pre></div></div>
            
        </li>
        
        
        <li id="section-12">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-12">&#182;</a>
              </div>
              <p>Replace common keywords.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        html = <span class="property">@parseTemplate</span> html, {to: toName}</pre></div></div>
            
        </li>
        
        
        <li id="section-13">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-13">&#182;</a>
              </div>
              <p>Set the message options.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        options.from = fromAddress
        options.to = toAddress
        options.subject = subject
        options.html = html</pre></div></div>
            
        </li>
        
        
        <li id="section-14">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-14">&#182;</a>
              </div>
              <p>Send the email.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        smtp.sendMail options, (err, result) -&gt;
            <span class="keyword">if</span> err?
                logger.error <span class="string">"Mail.send"</span>, <span class="string">"Could not send message: <span class="subst">#{subject}</span> to <span class="subst">#{toAddress}</span>"</span>, err
            <span class="keyword">else</span> <span class="keyword">if</span> settings.General.debug
                logger.info <span class="string">"Mail.send"</span>, subject, <span class="string">"to <span class="subst">#{toAddress}</span>"</span>, <span class="string">"from <span class="subst">#{fromAddress}</span>"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-15">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-15">&#182;</a>
              </div>
              <h2>TEMPLATES</h2>

            </div>
            
        </li>
        
        
        <li id="section-16">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-16">&#182;</a>
              </div>
              <p>Load the specified template from the cache or from the disk if it wasn&#39;t loaded yet.
Templates are stored inside the <code>/server/emailtemplates</code> folder by default,
and should have a .html extension.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    getTemplate: (name) =&gt;
        <span class="keyword">if</span> templateCache[name]?
            <span class="keyword">return</span> templateCache[name]</pre></div></div>
            
        </li>
        
        
        <li id="section-17">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-17">&#182;</a>
              </div>
              <p>Read base and <code>name</code> template and merge them together.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        base = fs.readFileSync <span class="string">"<span class="subst">#{settings.Path.emailTemplatesDir}</span>base.html"</span>
        template = fs.readFileSync <span class="string">"<span class="subst">#{settings.Path.emailTemplatesDir}</span><span class="subst">#{name}</span>.html"</span>
        result = base.toString().replace <span class="string">"{contents}"</span>, template.toString()</pre></div></div>
            
        </li>
        
        
        <li id="section-18">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-18">&#182;</a>
              </div>
              <p>Save to cache.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        templateCache[name] = result

        <span class="keyword">return</span> result</pre></div></div>
            
        </li>
        
        
        <li id="section-19">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-19">&#182;</a>
              </div>
              <p>Parse the specified template and replace <code>keywords</code> with the correspondent values of
the <code>obj</code> argument. For example if keywords is <code>{id: 1, friendlyUrl: &quot;abc&quot;}</code> then the tags
<code>{id}</code> and <code>{friendlyUrl}</code> will be replaced with the values 1 and abc.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    parseTemplate: (template, keywords) =&gt;
        template = template.toString()

        <span class="keyword">for</span> key, value <span class="keyword">of</span> keywords
            template = template.replace <span class="keyword">new</span> RegExp(<span class="string">"\\{<span class="subst">#{key}</span>\\}"</span>, <span class="string">"gi"</span>), value

        <span class="keyword">return</span> template</pre></div></div>
            
        </li>
        
        
        <li id="section-20">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-20">&#182;</a>
              </div>
              <h2>Singleton implementation</h2>

            </div>
            
        </li>
        
        
        <li id="section-21">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-21">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>Mail.<span class="function"><span class="title">getInstance</span></span> = -&gt;
    <span class="property">@instance</span> = <span class="keyword">new</span> Mail() <span class="keyword">if</span> <span class="keyword">not</span> <span class="property">@instance</span>?
    <span class="keyword">return</span> <span class="property">@instance</span>

module.exports = exports = Mail.getInstance()</pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>
